package com.yanqu.road.dao.impl.crosschannel;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.rank.xrank.XChannelRankPart;
import com.yanqu.road.entity.rank.xrank.XUserRank;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class XChannelRankPartDaoImpl extends TempDao {

    private String dbName;
    private String tableName;

    public XChannelRankPartDaoImpl(String dbName, String tableName) {
        this.dbName = dbName;
        this.tableName = tableName;
    }

    public List<XChannelRankPart> getXChannelRankPartList(int activityId) {

        String getXChannelRankPartSql = "select * from `" + tableName + "` where `activityId`=?";

        List<XChannelRankPart> list = new ArrayList<>();

        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(dbName);
            call = getDb().getConnection().prepareCall(getXChannelRankPartSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public boolean replaceXChannelRankPart(List<XChannelRankPart> rankList, Set<Integer> dbSet) {
        String insertXChannelRankPartSql = "replace into `" + tableName + "`(activityId,conditionType,channelId,rank,userId,nickname,value,updateTime) values (?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(dbName);
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            int rank = 0;
            for (XChannelRankPart xChannelRankPart : rankList) {
                rank++;
                if (!dbSet.contains(rank)) {
                    continue;
                }
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
                para.put(i++, new DbParameter(Types.INTEGER, xChannelRankPart.getActivityId()));
                para.put(i++, new DbParameter(Types.INTEGER, xChannelRankPart.getConditionType()));
                para.put(i++, new DbParameter(Types.INTEGER, xChannelRankPart.getChannelId()));
                para.put(i++, new DbParameter(Types.INTEGER, rank));
                para.put(i++, new DbParameter(Types.BIGINT, xChannelRankPart.getUserId()));
                para.put(i++, new DbParameter(Types.VARCHAR, xChannelRankPart.getNickname()));
                para.put(i++, new DbParameter(Types.VARCHAR, xChannelRankPart.getValue().toString()));
                para.put(i++, new DbParameter(Types.BIGINT, xChannelRankPart.getUpdateTime()));
                mapList.add(para);
            }
            if (getDb().executeBatchQuery(insertXChannelRankPartSql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    protected XChannelRankPart parse(ResultSet rs) throws SQLException {
        XChannelRankPart xChannelRankPart = new XChannelRankPart();
        xChannelRankPart.setActivityId(rs.getInt("activityId"));
        xChannelRankPart.setConditionType(rs.getInt("conditionType"));
        xChannelRankPart.setChannelId(rs.getInt("channelId"));
        xChannelRankPart.setUserId(rs.getLong("userId"));
        xChannelRankPart.setNickname(rs.getString("nickname"));
        xChannelRankPart.setValue(new BigInteger(rs.getString("value")));
        xChannelRankPart.setUpdateTime(rs.getLong("updateTime"));
        return xChannelRankPart;
    }

}
